<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
<script>
    function Queue(){
        this.data=[];
    }
    Queue.prototype={
        constructor:Queue,
        enqueue: function (elm) {//入队
            this.data.push(elm);
        },
        dequeue: function () {//删除队列中优先级最高（优先级代码最小）的元素
            var c= 5,l=this.data.length;
            if(!l)return 'No queue';
            do{
                for(var i=0;i<l;i++){
                    if(c==this.data[i].code){
                        return this.data.splice(i,1)[0];
                    }
                }
                c--;
            }while(c);
            return 'No priority';
        },
        front: function () {//获取队首元素
            return this.data[0];
        },
        back: function () {//获取队尾元素
            return this.data[this.length-1];
        },
        clear: function () {//清空
            delete this.data;
            this.data=[];
        },
        isEmpty: function () {//是否为空
            return this.data.length==0?true:false;
        },
        toString: function () {//显示
            var str='';
            for(var i= 0,l=this.data.length;i<l;i++){
                str+='name:'+this.data[i].name+',code:'+this.data[i].code+'\n';
            }
            return str;
        }
    }


    function Patient(name,code){
        this.name=name;
        this.code=code;
    }

    var ed=new Queue();

    ed.enqueue(new Patient('p4',4));
    ed.enqueue(new Patient('p11',1));
    ed.enqueue(new Patient('p3',3));
    ed.enqueue(new Patient('p51',5));
    ed.enqueue(new Patient('p12',1));
    ed.enqueue(new Patient('p52',5));
    ed.enqueue(new Patient('p2',2));

    console.log(ed.toString());

    var seen=ed.dequeue();
    console.log('Patient:',seen);

    var seen=ed.dequeue();
    console.log('Patient:',seen);

    var seen=ed.dequeue();
    console.log('Patient:',seen);

    var seen=ed.dequeue();
    console.log('Patient:',seen);

    var seen=ed.dequeue();
    console.log('Patient:',seen);

    var seen=ed.dequeue();
    console.log('Patient:',seen);

    var seen=ed.dequeue();
    console.log('Patient:',seen);

    var seen=ed.dequeue();
    console.log('Patient:',seen);

</script>
</body>
</html>